package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Client;
import Database.DatabaseConnector;

@WebServlet("/ClientEditServlet")
public class ClientEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DatabaseConnector dbConnect = new DatabaseConnector();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		String busName = (String) session.getAttribute("username");
		String ID = (String) req.getParameter("clientID");
		
		try{
			long clientID = Long.parseLong(ID);
			Client client = dbConnect.getClient(clientID, busName);
			
			JSONObject result = new JSONObject();
			try {
				if (client != null) {
					result.put("first", client.getFirstName());
					result.put("last", client.getLastName());
					result.put("tel1", client.getTel1() );
					result.put("tel2", client.getTel2() );
					result.put("email", client.getEmail() );
					result.put("city", client.getCity() );
					result.put("postal", client.getPostalCode() );
					result.put("province", client.getProvince() );
					result.put("street", client.getStreetAd() );
					result.put("cell", client.getCell() );
					result.put("clientID", clientID);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			res.getWriter().print(result);
			res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		}
		catch(NumberFormatException  e)
		{
			e.printStackTrace();
		}
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
